package com.whl.sm.controller.echart;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.expense.MaterBill;
import com.whl.sm.model.expense.TenancyBill;
import com.whl.sm.model.inhabitant.AssetsOwner;
import com.whl.sm.model.inhabitant.AssetsTenant;
import com.whl.sm.model.parking.ParkingLots;
import com.whl.sm.model.parking.ParkingRecord;
import com.whl.sm.model.repair.RepairApply;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.expense.MaterBillService;
import com.whl.sm.service.expense.TenancyBillService;
import com.whl.sm.service.inhabitant.AssetsOwnerService;
import com.whl.sm.service.inhabitant.AssetsTenantService;
import com.whl.sm.service.parking.ParkingLotsService;
import com.whl.sm.service.parking.ParkingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eChart")
public class EChartController extends BaseController {

    @Autowired
    private AssetsOwnerService assetsOwnerService;

    @Autowired
    private AssetsTenantService assetsTenantService;

    @Autowired
    private ComUnitsService comUnitsService;

    @Autowired
    private ParkingLotsService parkingLotsService;

    @Autowired
    private ParkingRecordService parkingRecordService;

    @Autowired
    private TenancyBillService tenancyBillService;

    @Autowired
    private MaterBillService materBillService;

    /*
    *   name: 名称
    *   value: 值
    * */
    @RequestMapping("/assets")
    public Result getAssetsInfo(){
        List<Map<String,Object>> resultList = new ArrayList<>();
        Integer owners = assetsOwnerService.count(new QueryWrapper<AssetsOwner>()
                .eq("owner_type",1)
                .ne("is_del",1)
        );
        Integer ownersObtain = assetsOwnerService.count(new QueryWrapper<AssetsOwner>()
                .eq("owner_type",0)
                .ne("is_del",1)
        );
        Integer tenancy = assetsTenantService.count(new QueryWrapper<AssetsTenant>()
                .eq("visible",1)
                .ne("is_del",1)
        );
        Map<String,Object> ownerMap = new HashMap<>(); ownerMap.put("name","小区业主"); ownerMap.put("value",owners);
        Map<String,Object> ownerObtainMap = new HashMap<>(); ownerObtainMap.put("name","小区住户"); ownerObtainMap.put("value",ownersObtain);
        Map<String,Object> tenancyMap = new HashMap<>(); tenancyMap.put("name","小区租户"); tenancyMap.put("value",tenancy);

        resultList.add(ownerMap); resultList.add(ownerObtainMap); resultList.add(tenancyMap);
        return Result.ok(resultList);
    }

    /*
     *   name: 名称
     *   value: 值
     * */
    @RequestMapping("/units")
    public Result getUnitsInfo(){
        List<Map<String,Object>> resultList = new ArrayList<>();
        Integer purchase = comUnitsService.count(new QueryWrapper<ComUnits>()
                .eq("unit_status",1)
                .ne("is_del",1)
        );
        Integer tenancy = comUnitsService.count(new QueryWrapper<ComUnits>()
                .eq("unit_status",2)
                .ne("is_del",1)
        );
        Integer free = comUnitsService.count(new QueryWrapper<ComUnits>()
                .eq("unit_status",0)
                .ne("is_del",1)
        );
        Map<String,Object> purchaseMap = new HashMap<>(); purchaseMap.put("name","出售"); purchaseMap.put("value",purchase);
        Map<String,Object> tenancyMap = new HashMap<>(); tenancyMap.put("name","租赁"); tenancyMap.put("value",tenancy);
        Map<String,Object> freeMap = new HashMap<>(); freeMap.put("name","空闲"); freeMap.put("value",free);

        resultList.add(purchaseMap); resultList.add(tenancyMap); resultList.add(freeMap);
        return Result.ok(resultList);
    }

    @RequestMapping("/parking")
    public Result getParkingInfo(){
        List<ParkingLots> parkingLots = parkingLotsService.list(new QueryWrapper<ParkingLots>()
                .ne("is_del",1)
        );
        parkingLots.stream().forEach(obj->{
            Integer hasCount =  parkingRecordService.count(new QueryWrapper<ParkingRecord>()
                    .eq("parking_lots_id",obj.getId())
                    .eq("visible",1)
            );
            obj.setHasCount(hasCount);
        });
        return Result.ok(parkingLots);
    }


    @RequestMapping("/materBill")
    public Result getMaterBillInfo(){
        List<Map<String,Object>> resultList = new ArrayList<>();
        Integer hasPay = materBillService.count(new QueryWrapper<MaterBill>()
                .eq("payment_status",1)
                .ne("is_del",1)
        );
        Integer hasNotPay = materBillService.count(new QueryWrapper<MaterBill>()
                .eq("payment_status",0)
                .ne("is_del",1)
        );
        Map<String,Object> hasPayMap = new HashMap<>(); hasPayMap.put("name","已支付"); hasPayMap.put("value",hasPay);
        Map<String,Object> hasNotPayMap = new HashMap<>(); hasNotPayMap.put("name","未支付"); hasNotPayMap.put("value",hasNotPay);

        resultList.add(hasPayMap); resultList.add(hasNotPayMap);
        return Result.ok(resultList);
    }

    @RequestMapping("/tenancyBill")
    public Result getTenancyBillInfo(){
        List<Map<String,Object>> resultList = new ArrayList<>();
        QueryWrapper gbQw = new QueryWrapper();
        gbQw.select("bill_month,count(*) as number");//查询自定义列
        gbQw.ne("is_del",1);
        gbQw.groupBy("bill_month");
        gbQw.last("limit 0,6");
        List<Map<String,Object>> groupBy = tenancyBillService.listMaps(gbQw);
        groupBy.stream().forEach(obj->{
            String month_item = obj.get("bill_month").toString();
            Integer hasPay = tenancyBillService.count(new QueryWrapper<TenancyBill>()
                    .eq("bill_month",month_item)
                    .eq("payment_status",1)
                    .ne("is_del",1)
            );
            Integer hasNotPay = tenancyBillService.count(new QueryWrapper<TenancyBill>()
                    .eq("bill_month",month_item)
                    .eq("payment_status",0)
                    .ne("is_del",1)
            );
            Map<String,Object> insertItem = new HashMap<>();
            insertItem.put("product",month_item); insertItem.put("已支付",hasPay); insertItem.put("未支付",hasNotPay);
            resultList.add(insertItem);
        });
        return Result.ok(resultList);
    }



}
